package com.github.yealove.configgen.mapper;

import com.github.yealove.configgen.model.NacosItem;
import com.github.yealove.configgen.model.Result;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ResultMapper {
    @Insert("insert into cg_result(file_name,file_content,file_type,nacos_group,nacos_namespace,version,create_time) values (#{fileName},#{fileContent},#{fileType},#{nacosGroup},#{nacosNamespace},#{version},now()) ")
    int addResult(Result result);

    @Select("select * from cg_result where (file_name like concat('%',#{search},'%') or version like concat('%',#{search},'%')) and create_time >= #{startTime}  and create_time <= #{endTime} order by create_time desc")
    List<Result> searchResult(String search, String startTime, String endTime);

    @Select({
            "<script>",
            "select",
            "*",
            "from cg_result",
            "where id in",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<Result> getResultByIds(@Param("ids") List<Integer> rIds);

    @Insert("insert into cg_push_log(data_id, content, type, n_group, tenant, create_time, create_ip, old_content, version) " +
            "values(#{dataId}, #{content}, #{type}, #{nGroup}, #{tenant}, now(), #{createIp}, #{oldContent}, #{version})")
    void addPushLog(NacosItem nacosItem);

    @Delete("delete from cg_result")
    void removeAll();
}
